<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" :width="1400">
    <el-form
      class="mb-10px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="74px"
    >
      <el-form-item label="申购单号" prop="requisitionForm">
        <el-input
          v-model="queryParams.requisitionForm"
          placeholder="请输入申购单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="委外方式" prop="dictOutsourceMode">
        <el-select
          v-model="queryParams.dictOutsourceMode"
          placeholder="请选择委外方式"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions('dict_outsource_mode')"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部件名称" prop="partName">
        <el-input
          v-model="queryParams.partName"
          placeholder="请输入部件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="图纸图号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图纸图号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTableRef"
      v-loading="loading"
      :data="quoteData"
      border
      style="width: 100%"
      :max-height="400"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" align="center" type="index" fixed="left" width="80" />
      <el-table-column label="申购单号" align="center" prop="requisitionForm" width="120" />
      <el-table-column label="委外方式" align="center" prop="dictOutsourceMode" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DICT_OUTSOURCE_MODE" :value="scope.row.dictOutsourceMode" />
        </template>
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="productNumber" width="120" />
      <el-table-column label="部件编号" align="center" prop="partNumber" width="120" />
      <el-table-column label="部件名称" align="center" prop="partName" width="120" />
      <el-table-column label="图纸图号" align="center" prop="drawingNumber" width="120" />
      <el-table-column label="部件规格" align="center" prop="partSpecification" width="120" />
      <el-table-column label="材质" align="center" prop="material" width="120" />
      <el-table-column label="规格" align="center" prop="specification" width="120" />
      <el-table-column label="净尺寸  (长)" align="center" prop="netSizeLength" width="120" />
      <el-table-column label="净尺寸  (宽)" align="center" prop="netSizeWidth" width="120" />
      <el-table-column label="下料尺寸(长)" align="center" prop="cuttingSizeLength" width="120" />
      <el-table-column label="下料尺寸(宽)" align="center" prop="cuttingSizeWidth" width="120" />
      <el-table-column label="计划数量" align="center" prop="planQuantity" width="120" />
      <el-table-column label="工序名称" align="center" prop="dictProcessName" width="120">
        <template #default="scope">
          <dict-tag type="craft_process_name" :value="scope.row.dictProcessName" />
        </template>
      </el-table-column>
      <el-table-column label="工种" align="center" prop="dictTypeOfWork" min-width="200px">
        <template #default="scope">
          <dict-tag
            type="config_manufacturing_details_cutting_process"
            :value="scope.row.dictTypeOfWork"
          />
        </template>
      </el-table-column>
      <el-table-column label="下料类型" align="center" prop="dictBaitingType" min-width="200px">
        <template #default="scope">
          <dict-tag type="config_wage_baiting_type" :value="scope.row.dictBaitingType" />
        </template>
      </el-table-column>
      <el-table-column label="是否机加" align="center" prop="dictIsMachine" min-width="200px">
        <template #default="scope">
          <dict-tag type="is_machining" :value="scope.row.dictIsMachine" />
        </template>
      </el-table-column>
      <el-table-column
        label="机加工序"
        align="center"
        prop="dictMachiningProcess"
        min-width="200px"
      >
        <template #default="scope">
          <dict-tag type="machining_process" :value="scope.row.dictMachiningProcess" />
        </template>
      </el-table-column>
      <el-table-column label="需求交期" align="center" prop="requirementTime" width="120" />
    </el-table>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as OutboundTaskOrderApi from '@/api/outsource/outsourceIndentManage/outboundTaskOrder'

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const quoteData = ref([]) // 机加数据
const queryParams = reactive({
  requisitionForm: undefined,
  dictOutsourceMode: undefined,
  partName: undefined,
  drawingNumber: undefined
})
const queryFormRef = ref() // 搜索的表单
const multipleTableRef = ref()
const ids = ref([])

/** 打开弹窗 */
const open = async (arr: []) => {
  ids.value = arr
  dialogVisible.value = true
  // 修改时，设置数据
  loading.value = true
  try {
    await getList()
    nextTick(() => {
      arr.map((val) => {
        quoteData.value.map((item) => {
          if (val === item['id']) {
            multipleTableRef.value!.toggleRowSelection(item, true)
          }
        })
      })
    })
  } finally {
    loading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 获取数据
const getList = async () => {
  loading.value = true
  try {
    const data = await OutboundTaskOrderApi.getOutboundTaskOrderFindAll(queryParams)
    quoteData.value = data
  } finally {
    loading.value = false
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  loading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', multipleSelection.value)
  } finally {
    loading.value = false
  }
}
// 多选
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
/** 搜索按钮操作 */
const handleQuery = () => {
  let id = ids.value as any
  open(id)
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
